<!DOCTYPE html>
<html>
<head>
    <title>SSE Example</title>
</head>
<body>
<h1>SSE Example</h1>
<div id="messages"></div>

<script>
    // 创建EventSource对象，与服务器的 /sse 接口建立连接
    const eventSource = new EventSource('http://localhost:7001/sse');

    // 监听消息事件，接收服务器推送的数据
    eventSource.onmessage = function(event) {
        const newElement = document.createElement("div");
        newElement.innerHTML = "Message: " + event.data;
        document.getElementById("messages").appendChild(newElement);
    };

    // 监听错误事件
    eventSource.onerror = function(event) {
        console.error("Error: ", event);
    };

    // 监听连接打开事件
    eventSource.onopen = function(event) {
        console.log("Connection opened.");
    };
</script>
</body>
</html>
